package com.yuan.java的String复习;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

import static java.lang.System.out;
/*
 *  @author 雨安
 *  类名： StringDemo1
 *  创建时间：2024/5/9
 */
public class StringDemo1 {
    public static void main(String[] args) throws InterruptedException {
        //creating a String 创建一个String
        String str = "Hello World!,";
        //拼接
        str += "Welcome to Java!";
        //打印他的长度
        out.println(str.length());
        //打印他的值
        out.println(str);
        //进行分割
        out.println(str.substring(0,7));
        //查找指定下标
        out.println(str.indexOf("l"));
        //判断是否与指定字符串值一样
        out.println(str.equals("Hello World!Welcome to Java!"));
        //将所有字符变成小写
        out.println(str.toLowerCase());
        //将所有字符变成大写
        out.println(str.toUpperCase(Locale.ROOT));
        //通过逗号拆分成为一个数组并且循环打印
        Arrays.stream(str.split(",")).forEach(out::println);
        //拼接
        String transform = str.transform((ele) -> "测试" + ele);
        //打印拼接
        out.println(transform);

        //str的锁
        Object lock = new Object();
        //异步线程
        Thread threadA = new Thread(() -> {
            synchronized (lock) {
                out.println("获取了锁");
                try {
                    out.println("休眠一会儿");
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                out.println(("wait前的线程状态" + Thread.currentThread().getState()));
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                out.println("被唤醒");
            }
        }, "A");
        threadA.start();



        Thread threadB = new Thread(()->{
            synchronized (lock) {
                out.println("获得了锁");
                out.println("线程A wait后的状态" + threadA.getState());
                lock.notify();
            }
        }, "B");
        threadB.start();


    }
}
